#include "parser.h"
#include "m02.h"
#include "sensor.h"

#include "../interface/mensagem.h"

#include <iostream>

using namespace  std;

M02::M02()
{
    init();

    sensores.push_back(new Sensor("Hora", "id", "DATETIME"));
    sensores.push_back(new Sensor("Temp Out", "temp_out", "REAL"));
    sensores.push_back(new Sensor("Hi Temp", "hi_temp", "REAL"));
    sensores.push_back(new Sensor("Low Temp", "low_temp", "REAL"));
    sensores.push_back(new Sensor("Out Hum", "out_hum", "REAL"));//
    sensores.push_back(new Sensor("Dew Pt.", "dew_pt", "REAL"));//
    sensores.push_back(new Sensor("Wind Speed", "wind_speed", "REAL"));
    sensores.push_back(new Sensor("Wind Dir", "wind_dir", "STRING"));
    sensores.push_back(new Sensor("Wind Run", "wind_run", "REAL"));
    sensores.push_back(new Sensor("Hi Speed.", "hi_speed", "REAL"));
    sensores.push_back(new Sensor("Hi Dir", "hi_dir", "STRING"));
    sensores.push_back(new Sensor("Wind Chill", "wind_chill", "REAL"));
    sensores.push_back(new Sensor("Heat Index", "heat_index", "REAL"));
    sensores.push_back(new Sensor("THW Index.", "thw_index", "REAL"));
    sensores.push_back(new Sensor("Bar", "bar", "REAL"));
    sensores.push_back(new Sensor("Rain", "rain", "REAL"));
    sensores.push_back(new Sensor("Rain Rate", "rain_rate", "REAL"));
    sensores.push_back(new Sensor("Heat D-D", "heat_d_d", "REAL"));
    sensores.push_back(new Sensor("Cool D-D", "cool_d_d", "REAL"));
    sensores.push_back(new Sensor("In Temp.", "in_temp", "REAL"));
    sensores.push_back(new Sensor("In Hum.", "in_hum", "REAL"));
    sensores.push_back(new Sensor("In Dew.", "in_dew", "REAL"));
    sensores.push_back(new Sensor("In Heat.", "in_heat", "REAL"));
    sensores.push_back(new Sensor("In EMC", "in emc", "REAL"));
    sensores.push_back(new Sensor("In Air Density", "in_air_density", "REAL"));
    sensores.push_back(new Sensor("Wind Samp", "wind_samp", "REAL"));
    sensores.push_back(new Sensor("Wind Tx", "wind_tx", "REAL"));
    sensores.push_back(new Sensor("ISS Recept", "iss_recept", "REAL"));
    sensores.push_back(new Sensor("Arc. Int.", "arc_int", "REAL"));

    QString aux;
    //cria tabela Estacao
    aux = "CREATE TABLE IF NOT EXISTS Estacao(id DATETIME NOT NULL, temp_out REAL, hi_temp REAL, low_temp REAL, out_hum REAL, dew_pt REAL, wind_speed REAL, wind_dir TEXT, wind_run REAL, hi_speed REAL, hi_dir TEXT, wind_chill REAL, heat_index REAL, thw_index REAL, bar REAL, rain REAL, rain_rate REAL, heat_d_d REAL, cool_d_d REAL, in_temp REAL, in_hum REAL, in_dew REAL, in_heat REAL, in_emc REAL, in_air_density REAL, wind_samp REAL, wind_tx REAL, iss_recept REAL, arc_int REAL, PRIMARY KEY(id));";
    db->exec(aux);
}

M02::~M02(){
    db->exec("DROP TABLE Estacao");
    db->close();
}

QStringList M02::parse(){ //insere valores na tabela
    QStringList lista;
    QString linha;
    QString insert;

    this->f->open(QIODevice::ReadOnly); // abre o arquivo somente para leitura
    if (!this->f->exists()){
        Mensagem::erro("Erro", "Arquivo não existe!");

        return lista;
    }


    //Consome as linhas de cabecalho
    linha = f->readLine();
    linha = f->readLine();
    linha = f->readLine();

    db->openTransaction();
    while (!f->atEnd()){ //enquanto nao encontra o fim do arquivo insere os valores na tabela e le a proxima linha
        insert = "INSERT INTO Estacao VALUES(";

        linha=linha.trimmed();
        linha=linha.replace(",",".");
        lista = linha.split(QRegExp("\\t"));

        QStringList relogio = lista.at(1).split(QRegExp("\\s"));

        QString dateTmp = lista.at(0) + " " + relogio.at(0);

        QDateTime dateTime = QDateTime::fromString(dateTmp, "dd/MM/yy h:mm");
        dateTime = dateTime.addYears(100);
        if (!relogio.at(1).compare("p"))
            dateTime = dateTime.addSecs(43200);

        QString date = "'" + dateTime.toString("yyyy-MM-dd hh:mm:ss") +"'";

        lista.pop_front();
        lista.pop_front();
        lista.push_front(date);

        QString bob;
        bob = lista.at(7);
        bob.push_back("\"");
        bob.push_front("\"");
        lista.replace(7,bob);

        bob = lista.at(10);
        bob.push_back("\"");
        bob.push_front("\"");
        lista.replace(10,bob);

        QString resultado = lista.join(" , ");


        insert+=resultado+");";
//        cout << insert.toStdString() << endl;

        db->exec(insert);
//        qDebug() << "erro ao inserir dados." << db.lastError();
        linha = f->readLine();
    }
    db->endTransaction();

    f->close();
    return lista;
}
